package com.example.zpositionanimationdemo.adapter;

import com.example.zpositionanimationdemo.ResourceTable;
import com.example.zpositionanimationdemo.bean.HistoryBean;
import com.example.zpositionanimationdemo.slice.ProgrammaticallyAddSlice;
import com.sxu.shadowdrawable.ShadowDrawable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;

import java.util.List;

public class HistoryItemProvider extends BaseItemProvider {
    private List<HistoryBean> list;
    private AbilitySlice slice;
    private int layout;

    public HistoryItemProvider(List<HistoryBean> aList, AbilitySlice aSlice, int aLayout) {
        this.list = aList;
        this.slice = aSlice;
        this.layout = aLayout;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component comp;
        if (component == null) {
            comp = LayoutScatter.getInstance(slice).parse(layout, null, false);
        } else {
            comp = component;
        }
        HistoryBean historyBean = list.get(i);
       Text text = (Text) comp.findComponentById(ResourceTable.Id_text);
       text.setText(historyBean.getName());

        boolean isCheck_but = ProgrammaticallyAddSlice.isIsCheck_but();
        if (isCheck_but == false){
            ShadowDrawable.setShadowDrawable(text, Color.getIntColor("#FFFFFF"), dpToPx(0),
                    Color.getIntColor("#FFCACACA"), dpToPx(3), 0, 3);
        }else {
            ShadowDrawable.setShadowDrawable(text, Color.getIntColor("#FFFFFF"), dpToPx(0),
                    Color.getIntColor("#FFCACACA"), dpToPx(5), 0, 5);
        }
       return comp;
    }

    private int dpToPx(int dp) {
        double density = DisplayManager.getInstance().getDefaultDisplay(slice).get().getAttributes().densityPixels;
        return (int) ( dp * density + 0.5f);
    }
}